package com.gitee.wsl.platform

import java.io.File

/**
 * Return the application's total memory in bytes.
 *
 */
//actual fun Platform.maxMemory(): Long {
//    return Runtime.getRuntime().maxMemory()
//}

object JavaPlatform: Platform{

    override val fileSeparator:String = File.separator

    override val lineSeparator: String
        get() = System.lineSeparator()

    //val pathSeparator:Char = File.pathSeparatorChar

    override val current: PlatformType
        get() = PlatformType.OS.Unknown(Arch.UNKNOWN )

    val isRunningOnAndroid by lazy { isClassAvailable("android.content.Context") }

    fun isClassAvailable(qualifiedClassName: String): Boolean =
        getClassOrNull(qualifiedClassName) != null

    fun getClassOrNull(qualifiedClassName: String): Class<*>? =
        try {
            Class.forName(qualifiedClassName)
        } catch (ignored: Exception) {
            null
      }

    fun getEnvironmentVariables(): Map<String, String> =
        System.getenv() + System.getProperties().stringPropertyNames().associateWith { System.getProperty(it) }

    override fun readEnvironmentVariable(key: String): String? {
        return System.getProperty(key)
    }

    override fun onShutdown(dispose: () -> Unit) {
        Runtime.getRuntime().addShutdownHook(Thread { dispose() })
    }

}